<template>
  <div class="result_wrapper">
    <Header :headerInfo="componentInfoHeader" />
    <CenterControl pageType="result" @resultFile="initFiles" @hideHeader="hideHeader" :resultType="showType * 1" :resultPageInfo="resultPageInfo" />

    <MarkContent ref="MarkContent" :files="files" :base="base" :componentsInfo="components" :arrowShow="arrowShow" pageType="result" />
  </div>
</template>
<script>
import Header from "@/components/mark/Header";
import CenterControl from "@/components/mark/centerControl";
import MarkContent from "@/components/mark/MarkContent"
let self = null
export default {
  name: "ResultPage",
  data() {
    return {
      base: {
        batchId: "",
        taskId: "",
        markTaskId: "",
        recheck: null,
        step: 0
      },
      componentInfoHeader: {
        curName: "", // 文件名
        standardBtnShow: true, // 标注规范显示
        standardCnt: "", // 标注规范内容
        abandonBtnShow: false, // 重新认证按钮显示
        repulseBtnShow: false, // 打回按钮显示
        isRecheck: false, // 是否是复检环节
        saveBtnShow: false, // 保存按钮显示
        historyBtnShow: false, // 历史按钮显示
        shortcutBtnShow: true, //  快捷键展示
        submitBtnShow: false,
        closeBtnShow: true,
        shortcutkeys: [], // 快捷键配置
        showVersionInfo: false // 展示历史按钮
      },
      files: {
        // 文件信息
        lastIndex: 0, // 上次标注的文件id
        allInfo: [], // 全部数据
        showType: 0, // 0 标注数据 1 检查数据，2 质检数据
        showList: [],
        current: null, // 当前的数据
        curIndex: 0, // 当前数据的id
        allImage: [] // 全部的图片
      }, // 文件相关
      showType: 0, // 结果页展示结果环节
      arrowShow: false,
      allResult: {},
      components: {}, // 组件信息
      resultPageInfo: {
        showMark: false,
        showCheck: false,
        showTest: false,
      }
    };
  },
  async created() {
    self = this;
    //获取taskId
    document.title = "结果页面"
    this.base.taskId = this.Utils.getQueryStringArgs()["taskId"];
    this.base.markTaskId = this.Utils.getQueryStringArgs()["markTaskId"];
    this.base.fileId = this.Utils.getQueryStringArgs()["fileid"];
    var params = {
      taskId: this.base.taskId,
      markTaskId: this.base.markTaskId,
      fileId: this.base.fileId
    };
    // return;
    
    let resultInfoPromise = await self.ass_api.getResultBySDK(params);
    console.log(resultInfoPromise, 'e21y7ehgdagadgshj')
    if (!resultInfoPromise) return;
    let { step, result, markStandard, fileName, pageElement } = resultInfoPromise
    if (!(step == 0 || step == 1 || step == 2)) {
      this.ui.error("页面类型异常");
      return;
    }
    self.allResult = result;
    self.componentInfoHeader.standardCnt = markStandard ? markStandard : "--";
    self.componentInfoHeader.curName = fileName ? fileName : " -- ";

    if (step * 1 >= result.length) {
      step = result.length - 1;
    }
    step = result[step].step; // 针对没有检查环节 result返回length长度为2 step = 1 => 但没有检查环节
    self.initFiles(step);
    this.setCustomComData(pageElement)

    // 快捷键
    // this.shortcutKeyEvent();
  },
  mounted: function () {
  },
  computed: {
  },
  methods: {
    // 处理组件数据
    async setCustomComData(pageElementJson) {
      let pageElement = JSON.parse(pageElementJson)
      let batchComInfo = pageElement.components
      // let templateId = pageElement.templateId
      let templateId = this.Utils.getUrlTemplateId() // 从url中获取
      // 获取组件信息
      // const componentPath = await this.ass_api.getComponentsInfo({ id: templateId })

      // let path = componentPath && componentPath.path

      // let customComInfo = await this.ass_api.$axios(path, 'GET', {})
      // 获取组件数据
      let customComInfo = await this.ass_api.getComponentsData(templateId)
      // 获取组件数据失败
      if(!customComInfo || !customComInfo.components) return;

      let components = JSON.parse(customComInfo.components)
      // 叠加配置设置值
      components.forEach(item => {
        let { id } = item
        if (!batchComInfo[id]) return;
        let batchComStyle = batchComInfo[id].style
        let batchComConfig = batchComInfo[id].config
        Object.keys(batchComStyle).forEach(key => {
          item.style[key] = batchComStyle[key]
        })
        Object.keys(batchComConfig).forEach(key => {
          item.config[key] = batchComConfig[key]
        })
      })
      customComInfo.components = components
      this.components = customComInfo
    },
    /**
     * 数据初始化
     * @param files
     * @param lastIndex
     */
    initFiles(step) {
      // 遍历循环全部数据
      var indexId = "";
      this.allResult.forEach(function (item) {
        if (item.step == step) {
          indexId = item.markTaskFileId;
        }
      });

      this.showType = step;
      this.files.allInfo = [];

      this.allResult.forEach(item => {
        if (item.step == 0) {
          this.resultPageInfo.showMark = true;
        } else if (item.step == 1) {
          this.resultPageInfo.showCheck = true;
        } else if (item.step == 2) {
          this.resultPageInfo.showTest = true;
        }
        if (step == item.step) {
          // console.log('step',step)
          var dInfo = {};
          try {
            // 标注默认结果
            dInfo = JSON.parse(item.finalResult.resultText);
          } catch (e) {
            dInfo = [];
          }
          // console.log('initFiles1233',dInfo);
          if (this.Utils.isNotNull(item)) {
            this.files.allInfo.push({
              result: dInfo, // 把扁平数据树结构化 供前端逻辑使用
              isBad: item.finalResult.extend.stamp,
              name: item.name,
              id: indexId,
              status: item.status,
              isInit: 0, // 表示尚未从结果服务获取数据
              saveSuccess: true
            });
          }
        }
      })
      this.selectData(step); // 选择数据
    },
    /**
     *  选择区域数据
     * @param _type
     * @param lastIndex
     */
    selectData: function (step) {
      self.files.showType = step;
      var showList = [];

      showList = self.files.allInfo;
      self.files.showList = showList; // 给当前列表赋值
      self.selectFile(self.files.showList[0]);
    },
    /**
     * 选择单个文件
     * @param item 当前条的数据
     */
    selectFile: function (item) {
      if (self.Utils.isNull(item)) return false; // 如果空 返回
      self.files.curIndex = item.id;
      // // 这里先赋值 数据
      this.$set(self.files, "current", item);
      // // console.log(item)
      self.initFile(item);
    },
    /**
     * 初始化单个文件
     * @param item
     */
    initFile: function (item) {
      // 判断是否获取最新结果 并初当前加载参数
      this.$refs.MarkContent.$emit('initFile', item) // 通知子组件 重置组件
    },
    hideHeader(showType) {
      this.arrowShow = showType;
    },
  },
  components: {
    Header,
    CenterControl,
    MarkContent
  },
};
</script>